package com.zues.samples.service.Impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zues.samples.dao.UserDAO;
import com.zues.samples.model.PagerModel;
import com.zues.samples.model.User;
import com.zues.samples.service.UserService;
import com.zues.samples.service.modelVO.UserVO;
import com.zues.samples.util.ExcelUtils;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDAO userDAO;

	@Override
	public List<User> findAllUsers() throws Exception {
		return userDAO.findAllUsers();
	}

	@Override
	public void createUser(User user) throws Exception {
		userDAO.createUser(user);
	}

	@Override
	public User getUserById(String UserId) throws Exception {
		return userDAO.getUserById(UserId);
	}

	@Override
	public void editUser(User user) throws Exception {
		userDAO.editUser(user);

	}

	@Override
	public void deleteUser(int UserId) throws Exception {
		userDAO.deleteUser(UserId);

	}

	@Override
	public void saveUserByExcel(InputStream inputStream, String postfix) {
		Workbook wk = ExcelUtils.openWorkbook(inputStream, postfix);
		List<UserVO> allExcelUser = ExcelUtils.getStudentListByExcel(wk);
		List<User> entities = new ArrayList<User>();
		if (allExcelUser != null) {
			User entity = null;
			for (int i = 0; i < allExcelUser.size(); i++) {
				entity = new User();
				BeanUtils.copyProperties(allExcelUser.get(i), entity);
				// TO ROLE
				entities.add(entity);
			}
		}
		try {
			userDAO.save(entities);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public PagerModel<UserVO> findAllByPage() {
		return userDAO.findAllByPage();
	}

	@Override
	public void updateUserLoginFailCount(User user) throws Exception {
		userDAO.updateUserLoginFailCount(user);
		
	}

	@Override
	public void updateUserLastLockTime(User user) throws Exception {
		userDAO.updateUserLastLockTime(user);;
	}

}
